home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / djsquash.com / C_PART.C next >
Encoding:
Text File  |  1989-04-06  |  8.1 KB  |  264 lines

  1. int state,iptr,nchar,n_in_buf, need_cpress;
  2. char g_r_buf[320], g_cmp[360], *p_r_buf = &g_r_buf[0], *p_cmp = &g_cmp[0];
  3. char *beg_ras = "\x1b*r#A";
  4. char *ras_cmd = "\x1b*b";
  5. char *end_ras = "\x1b*rB";
  6. char *myitoa(int, char *);
  7. void flush();
  8. void flush2();
  9. void send (char *);
  10. void pchar(unsigned char);
  11.  
  12. c_init() {
  13.      need_cpress = state = iptr = 0;
  14.      }
  15. c_filt(int c) {
  16.      int unique, lenloc,in_unique;
  17.      int g, run, runmax = 127, put;
  18.      int i;
  19.      int n,p;
  20.      char *r_buf, *cmp;
  21.      r_buf = p_r_buf;
  22.      cmp = p_cmp;
  23.      switch (state)  {
  24.           case 0: {
  25.                if ( c ==  beg_ras[iptr]) {
  26.                     iptr++;
  27.                     if      (iptr == 3) state = 1;
  28.                     else if (iptr == 5) {
  29.                          /* have a complete start raster cmd */
  30.                          state = 2;
  31.              flush();          /* send out command */
  32.              need_cpress = 1;  /* postpone compress cmd */
  33.                          }
  34.                     }
  35.                else {
  36.                     if (iptr != 0) flush();
  37.                     pchar(c);
  38.                     }
  39.                break;
  40.                }
  41.           case 1: { /* looking for digit 0 or 1 in start raster */
  42.                if (c == '0' || c == '1') {
  43.                     beg_ras[iptr] = c;
  44.                     iptr++;
  45.                     state = 0;
  46.                     }
  47.                else if (c == 'A') {
  48.                     state = 2;
  49.                     flush();
  50.                     pchar(c);
  51.             need_cpress = 1;  /* postpone compress cmd */
  52.                     }
  53.                else {
  54.                     flush();
  55.                     pchar(c);
  56.                     state = 0;
  57.                     }
  58.                break;
  59.                }
  60.           case 2: { /* looking for transfer raster graphics cmd */
  61.                /* search for <esc>*bnnnW */
  62.                if ( c ==  ras_cmd[iptr]) {
  63.                     iptr++;
  64.                     if      (iptr == 3) {
  65.                          state = 3;
  66.                          nchar = 0;
  67.                          }
  68.                     }
  69.                else {
  70.                     pchar(c);
  71.                     if (iptr == 2 && c == 'r') { /* is it a <esc>*rB ? */
  72.                          state = 6;
  73.                          }
  74.                     if (iptr != 0) flush2();
  75.                     }
  76.                break;
  77.                }
  78.           case 3: { /* munching digits in raster command, looking for W */
  79.                if (c >='0' && c <= '9') {
  80.                     nchar = nchar*10 + (c-'0');
  81.                     }
  82.            else if (c == 'M' || c == 'm')
  83.             { /* oh oh - got a compress code */
  84.                     /* abandon compression - it may already be compressed */
  85.             send("\x1b*b");
  86.             send(myitoa(nchar,r_buf));
  87.             /* don't forget the 'M'!!!! */
  88.             pchar('M');
  89.             state = 12;
  90.             iptr = 0;
  91.             }
  92.                else if (c != 'W') {
  93.                     if (iptr != 0) flush();
  94.                     pchar(c);
  95.                     }
  96.                else { /* we have a complete command header */
  97.                     if (nchar != 0) {
  98.              state = 4;
  99.              n_in_buf = 0;
  100.              if (need_cpress) {
  101.                   send("\x1b*b2M");
  102.                   need_cpress = 0;
  103.                   }
  104.                          send("\x1b*b");
  105.                          }
  106.                     else {
  107.                          send("\x1b*bW");
  108.                          state = 2;
  109.                          iptr = 0;
  110.                          }
  111.                     }
  112.                break;
  113.                }
  114.           case 4: { /* putting a run in the buffer */
  115.                r_buf[n_in_buf++] = c;
  116.                if (n_in_buf == nchar) {
  117.                     unique = lenloc = in_unique = put = 0;
  118.                     for (g=0;g<nchar;) {
  119.                          for (run=1;(r_buf[g]==r_buf[g+run])
  120.                               && ( (g+run) < nchar)
  121.                               && ( run < runmax); run++ );
  122.                          if (run > 2) {
  123.                               if (in_unique) {
  124.                                    cmp[lenloc] = unique - 1;
  125.                                    }
  126.                               cmp[put++] = -(run-1);
  127.                               cmp[put++] = r_buf[g];
  128.                               in_unique = 0;
  129.                               g += run;
  130.                               }
  131.                          else {
  132.                               if (unique >= runmax) {
  133.                                    cmp[lenloc] = unique - 1;
  134.                                    in_unique = 0;
  135.                                    }
  136.                               if (!in_unique) {
  137.                                    lenloc = put;
  138.                                    cmp[put++] = 0; /* placeholder */
  139.                                    in_unique = 1;
  140.                                    unique = 0;
  141.                                    }
  142.                               cmp[put++] = r_buf[g];
  143.                               g++;
  144.                               unique++;
  145.                               }
  146.                          }
  147.                     if (in_unique) { /* fix up any leftover unique */
  148.                          cmp[lenloc] = unique + 1;
  149.                          }
  150.             /* drop out the value of put */
  151.             send(myitoa(put,r_buf));
  152.             /* don't forget the 'W'!!!! */
  153.             pchar('W');
  154.             /* now put out 'put' bytes from cmp */
  155.             for (g=0;g<put;g++) pchar(cmp[g]);
  156.             state = 2;
  157.             iptr = 0;
  158.             }
  159.            break;
  160.                }
  161.           case 12: { /* dealing with compressed data, ignore until <esc>*rB */
  162.                /* search for <esc>*bnnnW */
  163.                pchar(c);
  164.                if ( c ==  ras_cmd[iptr]) {
  165.                     iptr++;
  166.                     if      (iptr == 3) {
  167.                          state = 13;
  168.                          nchar = 0;
  169.                          }
  170.                     }
  171.                else {
  172.                     if (iptr == 2 && c == 'r') { /* is it a <esc>*rB ? */
  173.              state = 16;
  174.                          }
  175.                     }
  176.                break;
  177.                }
  178.           case 13: { /* munching digits in raster command, looking for W */
  179.                pchar(c);
  180.                if (c >='0' && c <= '9') {
  181.                     nchar = nchar*10 + (c-'0');
  182.                     }
  183.                else if (c != 'W') { /* some other strange command ? */
  184.                     state = 12;
  185.                     iptr = 0;
  186.                     }
  187.                else { /* 'W' => we have a complete command header */
  188.                     if (nchar != 0) {
  189.              state = 14;
  190.                          }
  191.                     else {
  192.                          state = 12;
  193.                          iptr = 0;
  194.                          }
  195.                     }
  196.                break;
  197.                }
  198.           case 14: { /* putting a run in the buffer */
  199.                pchar(c);
  200.                if (n_in_buf == nchar) {
  201.             state = 12;
  202.             iptr = 0;
  203.             }
  204.            break;
  205.                }
  206.           case 6: { /* have received "<esc>*r",  is this char a B? */
  207.                pchar(c);
  208.                iptr = 0;
  209.            if ( c ==  'B')
  210.             {
  211.             state = 0;
  212.             }
  213.            else {
  214.             state = 2;
  215.             }
  216.                break;
  217.                }
  218.       case 16: { /* have received "<esc>*r",  is this char a B? */
  219.                pchar(c);
  220.                iptr = 0;
  221.            if ( c ==  'B')
  222.             {
  223.             state = 0;
  224.             }
  225.            else {
  226.             state = 12;
  227.             }
  228.                break;
  229.                }
  230.           }
  231.      }
  232. void flush() {
  233.      int i;
  234.      for (i=0; i<iptr; i++) pchar(beg_ras[i]);
  235.      iptr = 0;
  236.      }
  237. void flush2() {
  238.      int i;
  239.      for (i=0; i<iptr; i++) pchar(ras_cmd[i]);
  240.      iptr = 0;
  241.      }
  242. void send(char *s) {
  243.      char *c;
  244.      c = s;
  245.      while (*c) pchar(*c++);
  246.      }
  247. char *myitoa(int num, char *str) {
  248.      int n;
  249.      int p = 5;
  250.      n = num;
  251.      str[p--] = '\0';
  252.      while (n != 0) {
  253.       str[p--] = (n % 10) + '0';
  254.       n /= 10;
  255.       }
  256.      if (num == 0) str[p--] = '0';
  257.      p++;
  258.      do {
  259.       str[n++] = str[p++];
  260.       } while (str[p-1]);
  261.      return (str);
  262.      }
  263.  
  264.